// 
//  grid.jsx
//  Author: Liang Wang<bonede@gmail.com>
//  Lisence: http://www.opensource.org/licenses/mit-license.php
// 

// config
var documentWidth = 1000,
     documentHeight = 1000,
     documentResolution = 72,
     documentHorizontalPadding = 10;
     
// setting dialog
var settingDialogSpec = "dialog{text:'Grid Settings',frameLocation:[100,199],\
 row1: Group{ \
   st: StaticText{text:'Name:',preferredSize:[100,20]},\
   et:EditText{text:'Untitled',preferredSize:[100,20]},\
 },\
 row2: Group{ \
   st: StaticText{text:'Column Width:',preferredSize:[100,20]},\
   et:EditText{text:'30',preferredSize:[100,20]},\
 },\
 row3: Group{ \
   st: StaticText{text:'Gutter Width:',preferredSize:[100,20]},\
   et:EditText{text:'10',preferredSize:[100,20]},\
 },\
 row4: Group{ \
   st: StaticText{text:'Column Count:',preferredSize:[100,20]},\
   et:EditText{text:'24',preferredSize:[100,20]},\
 },\
 row5: Group{ \
   st1: StaticText{text:'Page Width:',preferredSize:[100,20]},\
   st2: StaticText{text:'950',preferredSize:[100,20]},\
 },\
 row6: Group{ \
   margin: 10,\
   alignChildren: 'right',\
   confirm_btn: Button{text:'OK'},\
   cancel_btn: Button{text:'Cancel'}\
 }\
}";

// UI elements
var settingDialog = new Window(settingDialogSpec),
    documentNameInput = settingDialog.children[0].children[1],
    columnWidthInput = settingDialog.children[1].children[1],
    gutterWidthInput = settingDialog.children[2].children[1],
    columnCountInput = settingDialog.children[3].children[1],
    pageWidthInput = settingDialog.children[4].children[1],
    okBtn = settingDialog.children[5].children[0];

pageWidthInput.text = pageWidthFor(columnWidthInput.text, gutterWidthInput.text, columnCountInput.text);

// events
columnWidthInput.onChanging = gutterWidthInput.onChanging = columnCountInput.onChanging = function(){
    pageWidthInput.text = pageWidthFor(columnWidthInput.text, gutterWidthInput.text, columnCountInput.text);
};

okBtn.onClick = function(){
  var newDocumentRef = documents.add( pageWidthFor(columnWidthInput.text, gutterWidthInput.text, columnCountInput.text) + documentHorizontalPadding*2,
           documentHeight,
           documentResolution,
           documentNameInput.text,
           NewDocumentMode.RGB,
           DocumentFill.WHITE
       );
   preferences.rulerUnits = Units.PIXELS;
   buildGuides(columnWidthInput.text, gutterWidthInput.text, columnCountInput.text);
   settingDialog.close();
}

settingDialog.show();

// functions
function pageWidthFor(columnWidth, gutterWith, columnCount){
    return columnWidth*columnCount + gutterWith*(columnCount - 1);
}
function buildGuides(columnWidth, gutterWith, columnCount){
    setGuide(documentHorizontalPadding,"Vrtc");
    var drawColumnRight = true,
         offset = documentHorizontalPadding;
    for( var i = 0 ; i < columnCount * 2 - 1; i++){
        if( drawColumnRight ){
            setGuide(offset + columnWidth*1,"Vrtc");
            offset += columnWidth*1;
            drawColumnRight = false;
        }else{
           setGuide(offset + gutterWith*1,"Vrtc");
           offset += gutterWith*1;
           drawColumnRight = true;
        }
    }
}
function setGuide(pxOffest, orientation){
    var desc = new ActionDescriptor();
    var desc1 = new ActionDescriptor();
    desc1.putUnitDouble( charIDToTypeID( "Pstn" ), charIDToTypeID( "#Pxl" ), pxOffest );
    desc1.putEnumerated( charIDToTypeID( "Ornt" ), charIDToTypeID( "Ornt" ), charIDToTypeID( orientation ) );// "Vrtc" or "Hrzn"
    desc.putObject( charIDToTypeID( "Nw  " ), charIDToTypeID( "Gd  " ), desc1 );
    executeAction( charIDToTypeID( "Mk  " ), desc, DialogModes.NO );
};